package edu.ccut.saturn.component.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ccut.saturn.component.ActionViewException;
import edu.ccut.saturn.component.IActionComponentInfo;
import edu.ccut.saturn.component.IActionContext;
import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnComponent;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.SaturnLoggerManager;
import edu.ccut.saturn.component.utils.ExceptionUtils;
import edu.ccut.saturn.component.utils.MathUtil;

/**
 * Action component use to implement component Type with action.
 * 
 * <p>
 * Action component like a action for handle a use request from web application.
 * It can invoke logic component for handle real business logic or invoke a
 * sub-action component. Finally, it return the next page path for servlet
 * dispatch by throw a {@link ActionViewException}
 * 
 * @author LeslieGu
 * 
 */
public class SaturnActionComponent extends SaturnComponent {

	private IActionComponentInfo actionComponentInfo;

	public SaturnActionComponent(IActionComponentInfo actionComponentInfo) {
		super(actionComponentInfo.getFullKey());
		this.actionComponentInfo = actionComponentInfo;
	}

	/**
	 * 
	 * @exception ActionViewException
	 */
	public void execute(IParameterContext parameterContext) throws Exception {
		
		IActionContext actionContext = this.createActionContext(parameterContext);
		
        this.loggerStart(actionContext);
		doExecute(actionContext);	
	}
	
	private void loggerStart(IActionContext actionContext) {
		logger.info("ACTION : { descn : '" + this.actionComponentInfo.getDescription() + "', key : '" 
				+ actionContext.getId() + "' }");		
	}

	private IActionContext createActionContext(
			IParameterContext parameterContext) {

		HttpServletRequest request = (HttpServletRequest) parameterContext
				.getParameterValue(0);

		HttpServletResponse response = (HttpServletResponse) parameterContext
				.getParameterValue(1);

		SaturnActionContext actionContext = new SaturnActionContext(
				this.actionComponentInfo.getFullKey() + " "+MathUtil.radom(), request, response);

		//logger.debug("ACTION CONTEXT CREATED!");

		return actionContext;
	}

	private void doExecute(IActionContext actionContext) throws Exception {
		INodeInfo startNodeInfo = this.actionComponentInfo.getStartNodeInfo();

		String nextId = null;
		INodeInfo nodeInfo = startNodeInfo;

		do {
			if (nodeInfo == null) {
				SaturnComponentException exception = new SaturnComponentException("can't find node id = " + nextId + "in action component " + this.actionComponentInfo.getFullKey());
				logger.error("ACTION ERROR : { message : '" + ExceptionUtils.getExMessage(exception) + "', process : '" + actionContext.getId() + "'}");		
				throw exception;
			}

			nodeInfo.execute(actionContext);
			nextId = actionContext.getNextNodeId(nodeInfo.getId());

			if (nextId == null) {
				nextId = nodeInfo.getNextId();

			} else {
				actionContext.removeExceptionRouter(nodeInfo.getId());
			}
			nodeInfo = this.actionComponentInfo.getNodeInfo(nextId);

		} while (nextId != null);
	}
}
